package com.xh.controller;


import com.xh.pojo.Article;
import com.xh.pojo.PageBean;
import com.xh.pojo.Result;
import com.xh.service.ArticleService;
import com.xh.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author 黄立恒
 * @version 1.0
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
//    @GetMapping("/list")
//    public Result<String> list(/*@RequestHeader(name = "Authorization") String token, HttpServletResponse response*/){
//        //验证token
//        try {
//            Map<String,Object> claims = JwtUtil.parseToken(token);
//            return Result.success("所有的商品简介...");
//        } catch (Exception e){
//            //http响应状态码401
//            response.setStatus(401);
//            return Result.error("未登录");
//        }
//         return Result.success("所有的商品简介...");
//    }

    @Autowired
    private ArticleService articleService;

    //添加商品
    @PostMapping
    public Result add(@RequestBody Article article){
        articleService.add(article);
        return Result.success();
    }

    //分页查询
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String sellstate
    ){
        PageBean<Article> pb = articleService.list(pageNum,pageSize,categoryId,state,sellstate);
        return Result.success(pb);
    }

    //更新商品
    @PutMapping
    public Result update(@RequestBody Article article){
        articleService.update(article);
        return Result.success();
    }

    //根据id获取商品信息
    @GetMapping("/detail")
    public Result<Article> detail(Integer id){
        Article a = articleService.findById(id);
        return Result.success(a);
    }

    //根据id删除商品信息
    @DeleteMapping
    public Result delete(Integer id){
        articleService.deleteById(id);
        return Result.success();
    }

}
